/**
 * 店铺费用记录API接口
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */

import { request } from '@/api/request'
import type { ApiResponse } from '@/types'

// 费用记录查询参数接口
export interface FeeRecordQueryParams {
  current?: number
  size?: number
  shopId?: number
  feeType?: string
  paymentStatus?: number
  feeDateStart?: string
  feeDateEnd?: string
  dueDateStart?: string
  dueDateEnd?: string
}

// 费用记录表单数据接口
export interface FeeRecordFormData {
  id?: number
  shopIds?: number[]
  shopId?: number
  ownerId?: number
  feeType: string
  feeAmount: number
  feePeriod: string
  feeStartDate: string
  feeEndDate: string
  paymentStatus: number
  paymentDate?: string
  paymentMethod?: string
  invoiceNumber?: string
  remark?: string
}

// 批量生成费用记录DTO
export interface BatchGenerateFeeRecordDTO {
  shopIds: number[]
  feeType: string
  batchMonth: string
  batchMode: 'STANDARD' | 'CUSTOM'
  customAmount?: number
  dueDate?: string
  paymentStatus: number
  remark?: string
}

// 费用记录详情接口
export interface FeeRecordDetail {
  id: number
  shopId: number
  shopName: string
  ownerId: number
  ownerName: string
  feeType: string
  feeTypeName: string
  feeAmount: number
  feePeriod: string
  feeStartDate: string
  feeEndDate: string
  paymentStatus: number
  paymentStatusName: string
  paymentDate?: string
  paymentMethod?: string
  invoiceNumber?: string
  remark?: string
  createTime: string
  updateTime: string
  createBy: number
  updateBy: number
}

// 分页响应接口
export interface FeeRecordPageResponse {
  records: FeeRecordDetail[]
  total: number
  current: number
  size: number
}

// 费用统计信息接口
export interface FeeStatistics {
  totalAmount: number
  paidAmount: number
  unpaidAmount: number
  overdueAmount: number
}

// 支付信息接口，遵循《Element Plus组件编码规范权威标准声明.md》规范
export interface PaymentInfo {
  paymentMethod: string
  paymentDate: string
  receiptNumber?: string
  remark?: string
}

// 批量支付请求接口
export interface BatchPaymentRequest {
  ids: number[]
  paymentInfo: PaymentInfo
}

/**
 * 店铺费用记录API
 * 遵循《Element Plus组件编码规范权威标准声明.md》API接口规范
 * 遵循《系统状态枚举权威标准声明.md》状态枚举规范
 */
export const shopFeeRecordApi = {
  /**
   * 分页查询费用记录列表
   * @param params 查询参数
   * @returns 分页结果
   */
  getPage: (params: FeeRecordQueryParams) => {
    return request.get<ApiResponse<FeeRecordPageResponse>>('/api/region/shop-fee-records/page', { params })
  },

  /**
   * 根据ID获取费用记录详情
   * @param id 费用记录ID
   * @returns 费用记录详情
   */
  getDetail: (id: number) => {
    return request.get<ApiResponse<FeeRecordDetail>>(`/api/region/shop-fee-records/${id}`)
  },

  /**
   * 创建费用记录
   * @param data 费用记录数据
   * @returns 操作结果
   */
  create: (data: FeeRecordFormData) => {
    return request.post<ApiResponse<number>>('/api/region/shop-fee-records', data)
  },

  /**
   * 批量生成费用记录
   * @param data 批量生成数据
   * @returns 操作结果
   */
  batchGenerate: (data: BatchGenerateFeeRecordDTO) => {
    return request.post<ApiResponse<any>>('/api/region/shop-fee-records/batch-generate', data)
  },

  /**
   * 更新费用记录
   * @param id 费用记录ID
   * @param data 费用记录数据
   * @returns 操作结果
   */
  update: (id: number, data: Partial<FeeRecordFormData>) => {
    return request.put<ApiResponse<boolean>>(`/api/region/shop-fee-records/${id}`, data)
  },

  /**
   * 删除费用记录
   * @param id 费用记录ID
   * @returns 操作结果
   */
  delete: (id: number) => {
    return request.delete<ApiResponse<boolean>>(`/api/region/shop-fee-records/${id}`)
  },

  /**
   * 批量删除费用记录
   * @param ids 费用记录ID列表
   * @returns 操作结果
   */
  batchDelete: (ids: number[]) => {
    return request.delete<ApiResponse<boolean>>('/api/region/shop-fee-records/batch', { data: ids })
  },

  /**
   * 获取费用统计信息
   * @param params 查询参数
   * @returns 统计信息
   */
  getStatistics: (params: {
    shopId?: number
    feeType?: string
    paymentStatus?: number
  }) => {
    return request.get<ApiResponse<FeeStatistics>>('/api/region/shop-fee-records/statistics', { params })
  },

  /**
   * 支付费用记录
   * @param id 费用记录ID
   * @param paymentInfo 支付信息
   * @returns 操作结果
   */
  pay: (id: number, paymentInfo: PaymentInfo) => {
    return request.post<ApiResponse<boolean>>(`/api/region/shop-fee-records/${id}/pay`, paymentInfo)
  },

  /**
   * 批量支付费用记录
   * @param data 批量支付数据
   * @returns 操作结果
   */
  batchPay: (data: BatchPaymentRequest) => {
    return request.post<ApiResponse<boolean>>('/api/region/shop-fee-records/batch-pay', data)
  },

  /**
   * 催缴费用记录
   * @param id 费用记录ID
   * @returns 操作结果
   */
  remind: (id: number) => {
    return request.post<ApiResponse<boolean>>(`/api/region/shop-fee-records/${id}/remind`)
  },

  /**
   * 批量催缴费用记录
   * @param ids 费用记录ID列表
   * @returns 操作结果
   */
  batchRemind: (ids: number[]) => {
    return request.post<ApiResponse<boolean>>('/api/region/shop-fee-records/batch-remind', ids)
  },

  /**
   * 导出费用记录
   * @param params 导出参数
   * @returns 导出结果
   */
  export: (params: {
    shopId?: number
    feeType?: string
    paymentStatus?: number
  }) => {
    return request.post<ApiResponse<string>>('/api/region/shop-fee-records/export', null, { params })
  }
}
